package top.z.work.module.system.controller.user;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.z.work.framework.common.pojo.PageInfo;
import top.z.work.framework.common.pojo.R;
import top.z.work.module.system.controller.user.vo.SystemUserCreateVO;
import top.z.work.module.system.controller.user.vo.SystemUserModifyVO;
import top.z.work.module.system.controller.user.vo.SystemUserVO;
import top.z.work.module.system.convert.SystemUserConvert;
import top.z.work.module.system.entity.SystemUser;
import top.z.work.module.system.service.SystemUserService;

import java.util.List;

/**
 * 用户管理Controller
 *
 * @author zaoangod
 * @since 2024-05-10 16:38:44
 */
@Tag(name = "用户管理")
@Slf4j
@Validated
@RestController
@RequestMapping("system/user")
public class SystemUserController {

    @Resource
    private SystemUserService systemUserService;

    @Operation(summary = "用户分页", description = "获取用户分页")
    @GetMapping("page")
    public R<PageInfo<SystemUserVO>> list(
            SystemUserVO parameter,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "size", defaultValue = "10") Integer size
    ) {
        QueryWrapper     query    = SystemUserConvert.INSTANCE.convert(parameter);
        Page<SystemUser> data     = Page.of(page, size);
        Page<SystemUser> pageInfo = systemUserService.page(data, query);
        return R.s(SystemUserConvert.INSTANCE.convert(pageInfo));
    }

    @Operation(summary = "用户列表", description = "获取用户列表")
    @GetMapping("list")
    public R<List<SystemUserVO>> list(SystemUserVO parameter) {
        QueryWrapper     query = SystemUserConvert.INSTANCE.convert(parameter);
        List<SystemUser> list  = systemUserService.list(query);
        return R.s(SystemUserConvert.INSTANCE.convert(list));
    }

    @Operation(summary = "用户信息", description = "获取用户信息")
    @GetMapping("info")
    public R<SystemUserVO> info(SystemUserVO parameter) {
        QueryWrapper query        = SystemUserConvert.INSTANCE.convert(parameter);
        SystemUser   systemConfig = systemUserService.getOne(query);
        return R.s(SystemUserConvert.INSTANCE.convert(systemConfig));
    }

    @Operation(summary = "删除用户", description = "根据用户ID删除数据")
    @DeleteMapping("remove")
    public R<Boolean> remove(@RequestParam("identity") @NotNull(message = "数据主键不能为空") Long identity) {
        boolean success = systemUserService.removeById(identity);
        return success ? R.s(success) : R.f();
    }

    @Operation(summary = "修改用户", description = "根据用户ID修改数据")
    @PutMapping("modify")
    public R<Boolean> modify(@RequestBody SystemUserModifyVO parameter) {
        SystemUser config  = SystemUserConvert.INSTANCE.convert(parameter);
        boolean    success = systemUserService.updateById(config);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "添加用户", description = "添加用户数据")
    @PostMapping("add")
    public R<Boolean> add(@RequestBody SystemUserCreateVO parameter) {
        SystemUser config  = SystemUserConvert.INSTANCE.convert(parameter);
        boolean    success = systemUserService.save(config);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "批量删除", description = "批量删除用户数据")
    @PostMapping("batch-remove")
    public R<Boolean> batchRemove(
            @RequestBody
            @NotNull(message = "数据主键列表不能为空")
            @NotEmpty(message = "数据主键列表不能为空")
            List<String> identityList
    ) {
        boolean success = systemUserService.removeByIds(identityList);
        return success ? R.s(true) : R.f();
    }

}
